from functools import partial
from PyQt5.QtWidgets import QPushButton

from ..vacwx import IVacWidget
from .pagevac import BasePageVac, EditorPageVac

class MainWndVac(IVacWidget):
    page_mapping = [
        ("首页", BasePageVac),
        ("PLC", BasePageVac),
        ("编辑模式", EditorPageVac),
    ]

    def __init__(self):
        super().__init__()

        self.create_pages()
        self._active()
        self.show()

    def create_pages(self):
        self.page_btn_list = []

        def initialize_window(index, title_btn_name, page_wx):
            page_btn = QPushButton(title_btn_name)
            page_btn.clicked.connect(partial(self.switch_page, index))
            self.page_stackedWidget.insertWidget(index, page_wx)
            self.page_btn_layout.addWidget(page_btn)
            self.page_btn_list.append(page_btn)

        index = -1
        for tuple_page_info in self.page_mapping:
            index += 1
            page_name = tuple_page_info[0]
            page_class = tuple_page_info[1]

            page = page_class()
            initialize_window(index, page_name, page)

        # 初始化首页
        self.switch_page(0)

    def switch_page(self, page_index):
        self.page_stackedWidget.setCurrentIndex(page_index)
